//
//  SEGCompleteGenderInfoViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/9.
//

#import "SEGCompleteGenderInfoViewModel.h"

@implementation SEGCompleteGenderInfoViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        // 默认选值操作
        self.recordSex = [SEGProfileManager sharedManager].profile.sex;
        [self seg_createInfoSignal];
    }
    return self;
}
- (void)seg_createInfoSignal {
    @weakify(self);
    self.infoSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       @strongify(self);
       [self.myManager requestgetCompleteInformationFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
           if (isSuccessed) {
               self.recordCoin = self.myManager.seg_segcompleteinfomodel.completeInformation;
               [subscriber sendNext:@""];
               [subscriber sendCompleted];
           } else {
               NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
               [subscriber sendError:error];
           }
       }];
        return nil;
    }];
}

- (void)seg_saveDataToUpdate {
    // NSDocumentDirectory 查找Documents文件夹
    // NSUserDomainMask 在用户目录下查找
    // YES 代表展开用户目录的~
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接文件路径   上面获取的文件路径加上文件名
    NSString *path = [doc stringByAppendingPathComponent:@"updateApartInfo.plist"];
    /*
     plist只能存储系统自带的一些常规的类, 也就是有writeToFile方法的对象才可以使用plist保存数据
     字符串/字典/数据/NSNumber/NSData ...
     */
    // 自定义的对象不能保存到plist中
    NSDictionary *dict = @{@"sex": self.recordSex};
    [dict writeToFile:path atomically:YES];
}

#pragma mark - Getter
- (SEGMyManager *)myManager {
    if (!_myManager) _myManager = [[SEGMyManager alloc] init];
    return _myManager;
}

@end
